<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="param.partsName" placeholder="请输入配件名称" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-input v-model="param.supplierName" placeholder="请输入供应商名称" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-input v-model="param.partsTypeName" placeholder="请输入配件分类" style="width: 200px;" class="filter-item"
                @keyup.enter.native="handleFilter"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="reset">重置</el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit"
                 @click="handleSaveData({})">添加
      </el-button>
      <el-button v-waves class="filter-item" type="danger" icon="el-icon-delete" @click="tableHandleBatchDelete">批量删除
      </el-button>
    </div>
    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;"
              @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"></el-table-column>
      <el-table-column label="配件名称" align="center" min-width="150px" prop="partsName"></el-table-column>

      <el-table-column label="配件图片" align="center" width="130px">
        <template slot-scope="{row}">
          <el-image class="table-img" :src="row.coverImg" :preview-src-list="[row.coverImg]"></el-image>
          <el-upload ref="upload"
                     :action="'http://localhost:8001' +moduleUrl + '/upload/' + row.id "
                     :on-success="handleAvatarSuccess"
                     :limit="1"
                     :before-upload="beforeImageUpload"
                     :show-file-list="false">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </template>
      </el-table-column>
      <el-table-column label="供应商名称" align="center" min-width="150px" prop="supplierName"></el-table-column>
      <el-table-column label="配件分类" align="center" min-width="150px" prop="partsTypeName"></el-table-column>
      <el-table-column label="单价" align="center" min-width="150px" prop="price"></el-table-column>
      <el-table-column label="库存" align="center" min-width="150px" prop="stock"></el-table-column>
      <el-table-column label="配件用途" align="center" min-width="150px" prop="content"></el-table-column>
      <el-table-column label="当前库存" align="center" min-width="150px" prop="currentStock"></el-table-column>

      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="{row,$index}">
          <el-button type="primary" size="mini" @click="handleSaveData(row)" icon="el-icon-edit">编辑</el-button>
          <el-button type="danger" size="mini" @click="handleTableDelete(row)" icon="el-icon-delete">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="param.current" :limit.sync="param.pageSize"
                @pagination="getList"/>
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="form" label-position="left" label-width="150px"
               style="width: 400px; margin-left:50px;">
        <el-form-item label="配件名称" prop="partsName">
          <el-input v-model="form.partsName"/>
        </el-form-item>
        <el-form-item label="所属供应商" prop="supplierId">
          <el-select v-model="form.supplierId" placeholder="请选择所属供应商">
            <el-option
                v-for="item in  SupplierData"
                :key="item.key"
                :label="item.value"
                :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input-number v-model="form.price" :min="1" :max="100000" :precision="2" :step="0.1"></el-input-number>
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input-number v-model="form.stock" :min="1" :max="100000" ></el-input-number>
        </el-form-item>
        <el-form-item label="配件用途" prop="content">
          <el-input type="textarea" v-model="form.content"></el-input>
        </el-form-item>
        <el-form-item label="所属分类" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择所属分类">
            <el-option
                v-for="item in  PartsTypeData"
                :key="item.key"
                :label="item.value"
                :value="item.key">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submitTableForm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import {tableUtil} from '../../utils/tableUtil';
import {batchDelete, page, saveData, getPartsTypeSelectData, getSupplierSelectData} from '@/api/parts/index'

export default {
  name: 'parts',
  components: {Pagination},
  directives: {waves},
  mixins: [tableUtil],
  data() {
    return {
      moduleUrl: '/parts',
      param: { // 列表查询参数
        current: 1,
        pageSize: 5,
        partsName: undefined,
        supplierName: undefined,
        partsTypeName: undefined,
      },
      form: { // 添加或修改提交表单
        id: undefined,
        partsName: undefined,
        supplierId: undefined,
        price: undefined,
        stock: undefined,
        coverImg: undefined,
        content: undefined,
        typeId: undefined,
        currentStock: undefined,
      },
      rules: { // 自定义校验规则
        partsName: [{required: true, message: '配件名称为必须项~', trigger: 'blur'}],
        supplierId: [{required: true, message: '所属供应商为必须项~', trigger: 'blur'}],
        price: [{required: true, message: '单价为必须项~', trigger: 'blur'}],
        stock: [{required: true, message: '库存为必须项~', trigger: 'blur'}],
        content: [{required: true, message: '配件用途为必须项~', trigger: 'blur'}],
        typeId: [{required: true, message: '所属分类为必须项~', trigger: 'blur'}],
      },
      PartsTypeData: [],
      SupplierData: [],
    }
  },
  created() {
    this.getList()
    this.getPartsTypeData()
    this.getSupplierData()
  },
  methods: {
    getList() { // 表格数据分页获取
      this.listLoading = true
      page(this.param).then(res => {
        this.list = res.data.records
        this.total = res.data.total
        this.listLoading = false
      })
    },
    submitTableForm() {
      this.submitForm(saveData)
    },
    handleTableDelete(row) {
      this.handleDelete(row, batchDelete)
    },
    tableHandleBatchDelete() {
      this.tableBatchDelete(batchDelete)
    },
    getPartsTypeData() {
      let that = this
      getPartsTypeSelectData().then(res => {
        that.PartsTypeData = res.data
      })
    },
    getSupplierData() {
      let that = this
      getSupplierSelectData().then(res => {
        that.SupplierData = res.data
      })
    },
  }
}
</script>
